package myJs.user

import myJs.Tool
import myJs.Utils._
import myJs.myPkg.Implicits._
import myJs.myPkg._
import myJs.myPkg.bootstrap.Bootstrap.default._
import myJs.myPkg.jquery._
import org.scalajs.dom.raw.{HTMLFormElement, HTMLInputElement}
import org.scalajs.dom.{FormData, _}
import scalatags.Text.all._

import scala.scalajs.js
import scala.scalajs.js.JSON
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import myJs.implicits.Implicits._

/**
 * Created by Administrator on 2020/1/17
 */
@JSExportTopLevel("Muscle")
object Muscle {

  val kind = "muscle"
  val eg =
    s"""
       |>Mar00695.mrna1
       |MDPNNCLLLNLKVYNFTNCNASNNSVDLPVNDDWFHPGILYVIPAIYGAIILIGLIGNIT
       |LIKIFCTVKSMRNVPNLFISSLALGDLLLLVTCAPVDASRYLADRWLFGRIGCKLIPFIQ
       |LTSVGVSVFTLTALSADRYKAIVRPMDIQTSHALLKICIKAALIWIISMLLAIPEAVFSD
       |LHPFHEESTNQTFISCAPYPHSNDLHPKIHSMASFLVFYIIPLSIISVYYYFIARNLIQS
       |AYNLPVEGNIHVKKQMESRKRLAKMVLVFVGLFAFCWLPNHVIYLYRSYHYSEVDTSMLH
       |FVTSICARLLAFTNSCVNPFALYLLSKTFRKQFNTQLLCCRPGLMSRSHSTGRSTTCMTS
       |FKSTNPSVATFSLINGNICHEGYV
       |>Mar00865.mrna1
       |MIKILTTTDGCTDWSVDIKKYQVLVGEPVRIKCALFYGYIRANYSLAQSAGLSLMWYKSS
       |GPGDFEEPIAFDGSRMSKEEDSIWFRPTLLQDSGLYACVIRNSTYCMKVSISLTVGENDT
       |GLCYNSKMKYFEKAELSKSKEISCRDIEDFLLPTREPEILWYKECRTKIWRPSIVFKRDT
       |LLIREVREDDIGNYTCELKYGGFVVRRTTELTEHLGEQEVSISLIVDSVEEGDLGNYSCY
       |VENGNGRRQASVLLHKRELMYTVELAGGLGAILLLLVCLVTIYKCYKIEIMLFYRNHFGA
       |EELDGDNKDYDAYLSYTKVDPDQWNQETGEEERFALEILPDMLEKHYGYKLFIPDRDLIP
       |TGTYIEDVARCVDQSKRLIIVMTPNYVVRRGWSIFELETRLRNMLVTGEIKVILIECSEL
       |RGIMNYQEVEALKHTIKLLTVIKWHGPKCNKLNSKFWKRLQYEMPFKRIEPITHEQALDV
       |SEQGPFGELQTVSAISMAAATSTALATAHPDLRSTFHNTYHSQMRQKHYYRSYEYDVPPT
       |GTLPLTSIGNQHTYCNIPMTLINGQRPQTKSSREQNPDEAHTNSAILPLLPRETSISSVI
       |W
       |""".stripMargin.toLines.mkString("\n")

  @JSExport("init")
  def init = {
    $("#missionTable").bootstrapTable()

    bootStrapValidator
    refreshMissionName
    exampleClick

  }

  def exampleClick = {
    $("#egQuery").click { () =>
      $("#queryText").`val`(eg)
      g.$("#form").bootstrapValidator("revalidateField", "queryText")
    }
  }

  @JSExport("showExampleFile")
  def showExampleFile = {
    val w = window.open()
    $(w.document.body).html(pre(eg).render)
  }

  def refreshMissionName = {
    $("input[name='missionName']").`val`(Genome.getCurrentTime)
  }

  def bootStrapValidator = {
    val maxNumber = Double.MaxValue
    val url = g.jsRoutes.controllers.GenomeController.missionNameCheck().url.toString
    val dict = js.Dictionary(
      "feedbackIcons" -> js.Dictionary(
        "valid" -> "glyphicon glyphicon-ok",
        "invalid" -> "glyphicon glyphicon-remove",
        "validating" -> "glyphicon glyphicon-refresh",
      ),
      "fields" -> js.Dictionary(
        "queryText" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "regexp" -> js.Dictionary(
              "regexp" -> js.RegExp("^(>.*\n[a-zA-Z\n]+\\s*){2,}$"),
              "message" -> "Sequences is invalid(at least two sequences)!"
            ),
          )
        ),
        "file" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "file" -> js.Dictionary(
              "extension" -> "txt",
              "message" -> "Sequences file must is a TXT file！"
            ),
          )
        ),
      )
    )
    g.$("#form").bootstrapValidator(dict)

  }


}
